package jsu.xxy.coffee3.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jsu.xxy.coffee3.Utils.MD5;
import jsu.xxy.coffee3.domain.RegisterNoId;
import jsu.xxy.coffee3.domain.RegisterRequest;
import jsu.xxy.coffee3.domain.Staffs;
import jsu.xxy.coffee3.mapper.RegisterRequestMapper;
import jsu.xxy.coffee3.service.StaffsService;
import jsu.xxy.coffee3.mapper.StaffsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 张从轩
* @description 针对表【staffs】的数据库操作Service实现
* @createDate 2023-06-03 04:07:54
*/
@Service

public class StaffsServiceImpl extends ServiceImpl<StaffsMapper, Staffs>
implements StaffsService{
    @Autowired
    StaffsMapper mapper;
    @Autowired
    RegisterRequestMapper registerRequestMapper;


    @Override
    public Boolean login(String phone, String password) {
        Staffs staff = mapper.searchBySphone(phone , MD5.getMD5(password),1);
        if(staff == null) return false;
        return true;
    }

    @Override
    public Boolean loginManage(String phone, String password) {
        Staffs staff = mapper.searchBySphone(phone ,  MD5.getMD5(password) , 0);
        if(staff == null) return false;
        return true;
    }

    @Override
    public IPage<Staffs> getAll(Integer pageNo, Integer pageSize) {
        Page<Staffs> page = new Page<>(pageNo, pageSize);
        IPage<Staffs> iPage = mapper.selectPage(page, null);
        return iPage;
    }

    @Override
    public IPage<Staffs> selectByName(String name, Integer pageNo, Integer pageSize) {
        QueryWrapper<Staffs> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name" , name);
        Page<Staffs> page = new Page<>(pageNo , pageSize);
        IPage<Staffs> iPage = mapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public IPage<Staffs> selectByPhone(String phone, Integer pageNo, Integer pageSize) {
        QueryWrapper<Staffs> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("phonenumber" , phone);
        Page<Staffs> page = new Page<>(pageNo , pageSize);
        IPage<Staffs> iPage = mapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public IPage<Staffs> selectById(int id, Integer pageNo, Integer pageSize) {
        QueryWrapper<Staffs> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staffid" , id);
        Page<Staffs> page = new Page<>(pageNo , pageSize);
        IPage<Staffs> iPage = mapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public IPage<Staffs> selectByStatus(String status, Integer pageNo, Integer pageSize) {
        QueryWrapper<Staffs> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status" , status);
        Page<Staffs> page = new Page<>(pageNo , pageSize);
        IPage<Staffs> iPage = mapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public Boolean change(Staffs staffs) {
        System.out.println(staffs);
        staffs.setPassword( MD5.getMD5(staffs.getPassword()));
        return mapper.update(staffs) > 0;
    }

    @Override
    public Boolean sign(RegisterNoId registerNoId) {
        System.out.println(registerNoId);
        registerNoId.setPassword(MD5.getMD5(registerNoId.getPassword()));
        if(registerRequestMapper.selectByphone_number(registerNoId.getPhonenumber()) == null)
            registerRequestMapper.insertNoId(registerNoId);
        else return false;
        return true;
    }


}
